<?php

class KhuyenMai_model extends CI_Model {

    function LayTatCaTinhThanhTrongNuoc() {
        $strSQL = "SELECT Distinct ks.Tinh_ThanhPho as Tinh_ThanhPho 
						FROM ThongTinKhuyenMai km 
							left join KhachSan ks on  km.KhachSan = ks.MaKS
							left join NhaCungCap ncc on ks.NhaCungCap = ncc.MaNCC
						WHERE ks.QuocGia='Việt Nam' and km.NgayKetThuc >= CURDATE() and ncc.TinhTrangTaiKhoan = 1
						ORDER BY Tinh_ThanhPho";
        $query = $this->db->query($strSQL);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return NULL;
    }

    function LayTatCaQuocGia() {
        $strSQL = "SELECT Distinct ks.QuocGia as QuocGia 
					FROM ThongTinKhuyenMai km 
					left join KhachSan ks on  km.KhachSan = ks.MaKS
					left join NhaCungCap ncc on ks.NhaCungCap = ncc.MaNCC
					WHERE km.NgayKetThuc >= CURDATE() and ncc.TinhTrangTaiKhoan = 1
					ORDER BY QuocGia";
        $query = $this->db->query($strSQL);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return NULL;
    }

    function LayTatCaKhuyenMaiConHan() {
        $strSQL = "SELECT km.MaKM, km.NoiDung as NoiDung,km.NgayKetThuc as NgayKetThuc, km.GiaTriKhuyenMai as TileKM, 
			min(lp.GiaTruocKhuyenMai) as Gia, lp.KhachSan as MaKS,ks.Ten as TenKS,ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,
			ks.QuocGia as QuocGia,ks.TieuChuan as TieuChuan,a.HinhLon as images
						FROM ThongTinKhuyenMai km 
						left join HinhAnh a on km.KhachSan = a.KhachSan 
						left join LoaiPhong lp on km.KhachSan = lp.KhachSan 
						left join KhachSan ks on km.KhachSan = ks.MaKS
						left join NhaCungCap ncc on ks.NhaCungCap = ncc.MaNCC
						where NgayKetThuc >= CURDATE() and ncc.TinhTrangTaiKhoan = 1
						group by lp.KhachSan";
        $query = $this->db->query($strSQL);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return NULL;
    }

    function LayKMTheoQuocGia($QuocGia) {
        $strSQL = "SELECT km.MaKM, km.NoiDung as NoiDung,km.NgayKetThuc as NgayKetThuc,km.GiaTriKhuyenMai as TileKM, 
			min(lp.GiaTruocKhuyenMai) as Gia, lp.khachsan as MaKS,ks.Ten as TenKS,ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,
			ks.QuocGia as QuocGia,ks.TieuChuan as TieuChuan,a.HinhLon as images
						FROM ThongTinKhuyenMai km 
						left join HinhAnh a on km.KhachSan = a.KhachSan 
						left join LoaiPhong lp on km.KhachSan = lp.KhachSan 
						left join KhachSan ks on km.KhachSan = ks.MaKS
						left join NhaCungCap ncc on ks.NhaCungCap = ncc.MaNCC
						WHERE QuocGia = '$QuocGia' and NgayKetThuc >= CURDATE() and ncc.TinhTrangTaiKhoan = 1
						group by lp.KhachSan";

        $query = $this->db->query($strSQL);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return NULL;
    }

    function LayKMTheoTinhThanhTrongNuoc($Tinh_Thanh) {
        $strSQL = "SELECT km.MaKM, km.NoiDung as NoiDung,km.NgayKetThuc as NgayKetThuc,km.GiaTriKhuyenMai as TileKM, 
			min(lp.GiaTruocKhuyenMai) as Gia, lp.khachsan as MaKS,ks.Ten as TenKS,ks.Quan as Quan,ks.Tinh_ThanhPho as Tinh_ThanhPho,
			ks.QuocGia as QuocGia,ks.TieuChuan as TieuChuan,a.HinhLon as images
						FROM ThongTinKhuyenMai km 
						left join HinhAnh a on km.KhachSan = a.KhachSan 
						left join LoaiPhong lp on km.KhachSan = lp.KhachSan 
						left join KhachSan ks on km.KhachSan = ks.MaKS
						left join NhaCungCap ncc on ks.NhaCungCap = ncc.MaNCC
						WHERE Tinh_ThanhPho='$Tinh_Thanh' and QuocGia = 'Việt Nam' and NgayKetThuc >= CURDATE() and ncc.TinhTrangTaiKhoan = 1
						group by lp.KhachSan";

        $query = $this->db->query($strSQL);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return NULL;
    }

}

?>